package com.yikes.yygh.controller;

import com.yikes.yygh.common.result.Result;
import com.yikes.yygh.controller.form.DictAddForm;
import com.yikes.yygh.controller.form.DictEditForm;
import com.yikes.yygh.service.DictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 组织架构表 前端控制器
 * </p>
 *
 * @author guanrong.yin
 * @since 2025-05-02
 */
@Tag(name = "DictController", description = "组织架构表")
@RestController
@RequestMapping("/admin/cmn/dict")
@RequiredArgsConstructor
public class DictController {

    private final DictService service;

    @Operation(description = "数据字典全列表")
    @GetMapping("/findAll/list")
    public Result findAll() {

        return Result.ok(service.findAll());
    }

    @Operation(description = "根据数据id查询子数据列表")
    @GetMapping("/findByParentId/{parentId}")
    public Result findByParentId(@PathVariable Long parentId) {

        return Result.ok(service.findByParentId(parentId));
    }

    @Operation(description = "查询当前id的数据信息")
    @GetMapping("/getInfo/{id}")
    public Result getInfoId(@PathVariable Long id) {

        return Result.ok(service.getInfoId(id));
    }

    @Operation(description = "新增")
    @PostMapping("/add")
    public Result add(@RequestBody DictAddForm form) {

        service.add(form);
        return Result.ok();
    }

    @Operation(description = "修改")
    @PutMapping("/edit")
    public Result edit(@RequestBody DictEditForm form) {

        service.edit(form);
        return Result.ok();
    }

    @Operation(description = "删除")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable Long id) {

        service.del(id);
        return Result.ok();
    }

    @Operation(description = "导出模板")
    @GetMapping("/export/template")
    public void exportTemplate(HttpServletResponse response) {

        service.exportTemplate(response);
    }

    @Operation(description = "导出")
    @GetMapping(value = "/exportData")
    public void exportData(HttpServletResponse response) {

        service.exportData(response);
    }

    @Operation(description = "导入")
    @PostMapping("/importData")
    public Result importData(MultipartFile file) {

        service.importData(file);
        return Result.ok();
    }

    @Operation(description = "获取数据字典名称")
    @GetMapping(value = "/getName/{parentDictCode}/{value}")
    public String getName(
            @Parameter(name = "parentDictCode", description = "上级编码", required = true)
            @PathVariable("parentDictCode") String parentDictCode,
            @Parameter(name = "value", description = "值", required = true)
            @PathVariable("value") String value) {

        return service.getNameByParentDictCodeAndValue(parentDictCode, value);
    }

    @Operation(description = "获取数据字典名称")
    @GetMapping(value = "/getName/{value}")
    public String getName(
            @Parameter(name = "value", description = "值", required = true)
            @PathVariable("value") String value) {
        return service.getNameByParentDictCodeAndValue("", value);
    }

}
